package com.aide.simplification.popedom;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.aide.simplification.global.db.DataBase;
import com.aide.simplification.popedom.login.LoginUser;
import com.aide.simplification.popedom.login.PopSSO;
import com.aide.simplification.popedom.popedom.base.PopedomOP;


public class Publiclass {
	private DataBase dataBase = new DataBase("pop");
	public int getLevel(String useruuid){
		int level = dataBase.queryForInt("select userlevel from pop_user where useruuid=?", new Object[]{useruuid});
		return level;
	}
	public List<Map<String, Object>> getUpUserByUser(String useruuid) {
		String sql = "select distinct u.useruuid,u.username,u.name,CAST(u.pop AS VARCHAR) as pop,u.userlevel from pop_user as u,pop_user_dept as ud, deptup('"
				+ useruuid
				+ "') as dup where u.state=1 and u.useruuid=ud.useruuid and ud.deptid=dup.deptid and u.userlevel>=?";
		return dataBase.queryForList(sql, new Object[] {this.getLevel(useruuid)});
	}

	public List<Map<String, Object>> getDownUserByUser(String useruuid) {
		String sql = "select distinct u.useruuid,u.username,u.name,CAST(u.pop AS VARCHAR) as pop,u.userlevel from pop_user as u,pop_user_dept as ud, deptdown('"
				+ useruuid
				+ "') as down where u.state=1 and u.useruuid=ud.useruuid and ud.deptid=down.deptid and u.userlevel<=?";
		return dataBase.queryForList(sql, new Object[] {this.getLevel(useruuid)});
	}

	public List<Map<String, Object>> getUpDeptByUser(String useruuid) {
		String sql = "select distinct d.deptid,d.dept,d.depttext from pop_dept as d,deptup('"
				+ useruuid + "') as dup where d.sign=1 d.deptid=dup.deptid and u.userlevel>=?";
		return dataBase.queryForList(sql, new Object[] {this.getLevel(useruuid)});
	}

	public List<Map<String, Object>> getDownDeptByUser(String useruuid) {
		String sql = "select distinct d.deptid,d.dept,d.depttext from pop_dept as d,deptdown('"
				+ useruuid
				+ "') as down where d.sign=1 and d.deptid=down.deptid and u.userlevel<=?";
		return dataBase.queryForList(sql, new Object[] {this.getLevel(useruuid)});
	}

	public String getDepts(String useruuid) {
		String sql = "select distinct d.deptid,d.dept,d.depttext from pop_dept as d,deptdown('"
				+ useruuid
				+ "') as down where d.sign=1 and d.deptid=down.deptid";
		String deptids = "";
		List<Map<String, Object>> list = dataBase.queryForList(sql,
				new Object[] {});
		for (Iterator<Map<String, Object>> iterator = list.iterator(); iterator
				.hasNext();) {
			Map<String, Object> map = iterator.next();
			deptids += "'" + map.get("deptid").toString() + "',";
		}
		deptids = deptids.substring(0, deptids.length() - 1);
		return deptids;
	}

	public List<Map<String, Object>> getDownUserByDept(String deptid) {
		String sql = "select distinct u.useruuid,u.username,u.name,CAST(u.pop AS VARCHAR) as pop,u.userlevel from pop_user as u,pop_user_dept as ud, deptdownBydept('"
				+ deptid
				+ "') as down where u.state=1 and u.useruuid=ud.useruuid and ud.deptid=down.deptid";
		return dataBase.queryForList(sql, new Object[] {});
	}

	public String getUseruuids(ServletRequest request,
			ServletResponse response, String pop1, String pop2) {
		PopSSO sso = new PopSSO(request, response);
		PopedomOP popop = new PopedomOP();
		LoginUser lu = sso.ssoSync();
		String useruuids = "";
		if (lu != null) {
			if (popop.checkPopAnd(pop1, lu.getPop())) {
				List<Map<String, Object>> list = this.getDownUserByUser(lu
						.getUseruuid());

				for (Iterator<Map<String, Object>> iterator = list.iterator(); iterator
						.hasNext();) {
					Map<String, Object> map = iterator.next();
					if (pop2.equals("0")
							|| pop2.equals("")
							|| popop.checkPopAnd(pop2, map.get("pop")
									.toString())) {
						useruuids += "'" + map.get("useruuid").toString()
								+ "',";
					}
				}
				useruuids = useruuids.substring(0, useruuids.length() - 1);
			} else {
				useruuids = "'" + lu.getUseruuid() + "'";
			}
		}
		return useruuids;
	}

	public String getUseruuidsUp(ServletRequest request,
			ServletResponse response, String useruuid, String pop) {
		PopSSO sso = new PopSSO(request, response);
		PopedomOP popop = new PopedomOP();
		LoginUser lu = sso.ssoSync();
		String useruuids = "";
		if (lu != null) {
			List<Map<String, Object>> list = this.getUpUserByUser(useruuid);
			for (Iterator<Map<String, Object>> iterator = list.iterator(); iterator
					.hasNext();) {
				Map<String, Object> map = iterator.next();
				if (pop.equals("0") || pop.equals("")
						|| popop.checkPopAnd(pop, map.get("pop").toString())) {
					useruuids += "'" + map.get("useruuid").toString() + "',";
				}
			}
			useruuids = useruuids.substring(0, useruuids.length() - 1);
		}
		return useruuids;
	}

	public String getUseruuidsDown(ServletRequest request,
			ServletResponse response, String useruuid, String pop) {
		PopSSO sso = new PopSSO(request, response);
		PopedomOP popop = new PopedomOP();
		LoginUser lu = sso.ssoSync();
		String useruuids = "";
		if (lu != null) {
			List<Map<String, Object>> list = this.getDownUserByUser(useruuid);
			for (Iterator<Map<String, Object>> iterator = list.iterator(); iterator
					.hasNext();) {
				Map<String, Object> map = iterator.next();
				if (pop.equals("0") || pop.equals("")
						|| popop.checkPopAnd(pop, map.get("pop").toString())) {
					useruuids += "'" + map.get("useruuid").toString() + "',";
				}
			}
			useruuids = useruuids.substring(0, useruuids.length() - 1);
		}
		return useruuids;
	}

	public String getUseruuidsByDeptid(ServletRequest request,
			ServletResponse response, String deptid, String pop) {
		PopedomOP popop = new PopedomOP();
		String useruuids = "";
		List<Map<String, Object>> list = this.getDownUserByDept(deptid);
		for (Iterator<Map<String, Object>> iterator = list.iterator(); iterator
				.hasNext();) {
			Map<String, Object> map = iterator.next();
			if (popop.checkPopAnd(pop, map.get("pop").toString())) {
				if (popop.checkPopAnd(pop, map.get("pop").toString())) {
					useruuids += "'" + map.get("useruuid").toString() + "',";
				}
			}
		}
		useruuids = useruuids.substring(0, useruuids.length() - 1);
		return useruuids;
	}
}
